<?php

namespace app\admin\model;

use think\Model;
use think\facade\Session;

class Admin extends Model
{
    public function login($username = '', $password = '')
    {
        $username = trim($username);
        $password = trim($password);
        $userInfo = $this->where('user',$username)->find();
        if (empty($userInfo) || empty($password)) {
            return false;
        }
        //密码验证
        if (encrypt_password($password) != $userInfo['pass']) {
            return false;
        }
        $auth = [
            'uid' => $userInfo['Id'],
            'username' => $userInfo['user']
        ];
        Session::set('admin_user', $auth);
        return true;
    }
    //修改管理员密码
    public function update_pass($old_pass,$new_pass){
        //旧密码和新密码不可以相同
        if($old_pass == $new_pass){
            return false;
        }
        if($this->where('Id','1')->find()['pass'] != encrypt_password($old_pass)){
            //旧密码不对
            return false;
        }
        if($this->where('Id','1')->find()['pass'] == encrypt_password($new_pass)){
            //相当于没改
            return false;
        }
        if($this->save(['pass'=>encrypt_password($new_pass)],['Id'=>1])){
            return true;
        }else{
            return false;
        }
    }


}
